package qianfeng.com.meijiadaka.adapter.shop_details;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.bean.detail.SpecialBeanBelow;

/**
 * Created by Think on 2017/2/7.
 * 美咖专题。评论适配器
 */

public class SpecialDetailsCommend1Adapter extends RecyclerView.Adapter<SpecialDetailsCommend1Adapter.MyViewHolder> {
    private List<Object> list;
    private SpecialBeanBelow bean;
    private Context context;
    OnItemClickListener mListener;
    private RecyclerView rv;


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        rv = recyclerView;
    }

    public interface OnItemClickListener{
        void onClick(int index);
    }
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mListener=itemClickListener;
    }


    public SpecialDetailsCommend1Adapter(SpecialBeanBelow bean, Context context) {
        this.bean = bean;
        this.context = context;
        list = new ArrayList<>();
        list.addAll(bean.getResult().getComments());
        Log.e("SpecialDetailsCommend1Adapter: ",list.size()+"");

    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(context).inflate(R.layout.specia_details_list_item4_rvitem,parent,false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener!=null){
                    mListener.onClick(rv.getChildAdapterPosition(view));
                }
            }
        });
        return new MyViewHolder(view);
    }

    //填充onCreateViewHolder方法返回的holder中的控件
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(bean.getResult().getComments().get(position).getAuthor());
        holder.tv1.setText(bean.getResult().getComments().get(position).getContent());

        holder.tv3.setText(bean.getResult().getComments().get(position).getLikeCount()+"");
        //头像
        Picasso.with(context).load(bean.getResult().getComments().get(position).getAuthorPhoto()+"")
                .placeholder(R.mipmap.ic_default_photo).into(holder.iv);


        if(bean.getResult().getComments().get(position).getIsHomework()==0){
            holder.tv2.setText(bean.getResult().getComments().get(position).getCreateDate());
        }else {
            Picasso.with(context).load(bean.getResult().getComments().get(position).getContentPic()).into(holder.iv1);
            holder.tv2.setText(bean.getResult().getComments().get(position).getCreateDate()+" 交作业");
        }
        if (bean.getResult().getComments().get(position).getReplys().size()>0){
            Log.e( "onBindViewHolder: ", "有评论"+bean.getResult().getComments().get(position).getReplys().size());
            LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
            holder.rv.setLayoutManager(manager);
            SpeaialDetailsCommend2RecyclerAdapter adapter = new SpeaialDetailsCommend2RecyclerAdapter(bean,context);
            holder.rv.setAdapter(adapter);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView iv,iv1;
        TextView tv,tv1,tv2,tv3;
        RecyclerView rv;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.specia_list4_item_ivhead);
            iv1 = (ImageView) itemView.findViewById(R.id.specia_list4_item_ivbig);
            tv = (TextView) itemView.findViewById(R.id.specia_list4_item_tv_name);
            tv1 = (TextView) itemView.findViewById(R.id.specia_list4_item_tv_commend);
            tv2 = (TextView) itemView.findViewById(R.id.specia_list4_item_tv_commend_date);
            tv3 = (TextView) itemView.findViewById(R.id.specia_list4_item_tv_commend_zan);
            rv = (RecyclerView) itemView.findViewById(R.id.specia_details_list4_rvitem_commend_recyclerView);
        }
    }
}
